/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.stream;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;

/**
 *
 * @author Pierre
 */
public class DataUtil {

    public static String read(Reader reader) {
        try {
            final StringBuilder sb = new StringBuilder();
            for (int codePoint = reader.read(); codePoint >= 0; codePoint =
                            reader.read()) {
                sb.appendCodePoint(codePoint);
            }
            return sb.toString();
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String readUTF8(URL url) {
        {

            try {
                final InputStream is = url.openStream();
                try {
                    return read(new InputStreamReader(
                            new BufferedInputStream(is),
                            "UTF-8"));
                } finally {
                    is.close();
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
